Enter the directory of the maca folder on your drive and the name of the tissue you want to analyze.
tissue_of_interest = "Thymus"
Load the requisite packages and some additional helper functions.
library(here)
here() starts at /Users/olgabot/code/tabula-muris
library(useful)
Loading required package: ggplot2
library(Seurat)
Loading required package: cowplot
Attaching package: 'cowplot'
The following object is masked from 'package:ggplot2':
ggsave
Loading required package: Matrix
Warning: namespace 'Biobase' is not available and has been replaced
by .GlobalEnv when processing object 'call.'
Warning: namespace 'lme4' is not available and has been replaced
by .GlobalEnv when processing object 'call.'
Warning: namespace 'MatrixModels' is not available and has been replaced
by .GlobalEnv when processing object 'call.'
Warning: namespace 'Biobase' is not available and has been replaced
by .GlobalEnv when processing object 'call.'
Warning: namespace 'lme4' is not available and has been replaced
by .GlobalEnv when processing object 'call.'
Warning: namespace 'MatrixModels' is not available and has been replaced
by .GlobalEnv when processing object 'call.'
library(dplyr)
Warning: package 'dplyr' was built under R version 3.4.2
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(Matrix)
save_dir = here('00_data_ingest', 'tissue_robj')
# read the metadata to get the plates we want
plate_metadata_filename = here('00_data_ingest', 'facs_raw_data', 'metadata_FACS.csv')
plate_metadata <- read.csv(plate_metadata_filename, sep=",", header = TRUE)
colnames(plate_metadata)[1] <- "plate.barcode"
plate_metadata
Subset the metadata on the tissue.
tissue_plates = filter(plate_metadata, tissue == tissue_of_interest)[,c('plate.barcode','tissue','subtissue','mouse.sex')]
tissue_plates
Load the read count data.
#Load the gene names and set the metadata columns by opening the first file
filename = here('00_data_ingest', 'facs_raw_data', 'FACS', paste0(tissue_of_interest, '-counts.csv'))
raw.data = read.csv(filename, sep=",", row.names=1)
# raw.data = data.frame(row.names = rownames(raw.data))
corner(raw.data)
Make a vector of plate barcodes for each cell
plate.barcodes = lapply(colnames(raw.data), function(x) strsplit(strsplit(x, "_")[[1]][1], '.', fixed=TRUE)[[1]][2])
head(plate.barcodes)
[[1]]
[1] "MAA000607"
[[2]]
[1] "MAA000607"
[[3]]
[1] "MAA000607"
[[4]]
[1] "MAA000607"
[[5]]
[1] "MAA000607"
[[6]]
[1] "MAA000607"
MAA000607
MAA000607
MAA000607
MAA000607
MAA000607
MAA000607
Use only the metadata rows corresponding to Bladder plates. Make a plate barcode dataframe to “expand” the per-plate metadata to be per-cell.
barcode.df = t.data.frame(as.data.frame(plate.barcodes))
rownames(barcode.df) = colnames(raw.data)
colnames(barcode.df) = c('plate.barcode')
head(barcode.df)
plate.barcode
H6.MAA000607.3_9_M.1.1 "MAA000607"
I8.MAA000607.3_9_M.1.1 "MAA000607"
J10.MAA000607.3_9_M.1.1 "MAA000607"
K12.MAA000607.3_9_M.1.1 "MAA000607"
A17.MAA000607.3_9_M.1.1 "MAA000607"
A16.MAA000607.3_9_M.1.1 "MAA000607"
MAA000607
MAA000607
MAA000607
MAA000607
MAA000607
MAA000607
rnames = row.names(barcode.df)
meta.data <- merge(barcode.df, plate_metadata, by='plate.barcode', sort = F)
row.names(meta.data) <- rnames
# Sort cells by plate barcode because that's how the data was originally
meta.data = meta.data[order(meta.data$plate.barcode), ]
corner(meta.data)
raw.data = raw.data[, rownames(meta.data)]
corner(raw.data)
Process the raw data and load it into the Seurat object.
# Find ERCC's, compute the percent ERCC, and drop them from the raw data.
erccs <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = TRUE)
percent.ercc <- Matrix::colSums(raw.data[erccs, ])/Matrix::colSums(raw.data)
ercc.index <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = FALSE)
raw.data <- raw.data[-ercc.index,]
# Create the Seurat object with all the data
tiss <- CreateSeuratObject(raw.data = raw.data, project = tissue_of_interest,
min.cells = 5, min.genes = 5)
tiss <- AddMetaData(object = tiss, meta.data)
tiss <- AddMetaData(object = tiss, percent.ercc, col.name = "percent.ercc")
# Change default name for sums of counts from nUMI to nReads
colnames(tiss@meta.data)[colnames(tiss@meta.data) == 'nUMI'] <- 'nReads'
# Create metadata columns for annotations and subannotations
tiss@meta.data[,'annotation'] <- NA
tiss@meta.data[,'subannotation'] <- NA
Calculate percent ribosomal genes.
ribo.genes <- grep(pattern = "^Rp[sl][[:digit:]]", x = rownames(x = tiss@data), value = TRUE)
percent.ribo <- Matrix::colSums(tiss@raw.data[ribo.genes, ])/Matrix::colSums(tiss@raw.data)
tiss <- AddMetaData(object = tiss, metadata = percent.ribo, col.name = "percent.ribo")
A sanity check: genes per cell vs reads per cell.
GenePlot(object = tiss, gene1 = "nReads", gene2 = "nGene", use.raw=T)
Filter out cells with few reads and few genes.
tiss <- FilterCells(object = tiss, subset.names = c("nGene", "nReads"),
low.thresholds = c(500, 50000), high.thresholds = c(25000, 2000000))
Normalize the data, then regress out correlation with total reads
tiss <- NormalizeData(object = tiss)
tiss <- ScaleData(object = tiss, vars.to.regress = c("nReads", "percent.ribo","Rn45s"))
[1] "Regressing out nReads" "Regressing out percent.ribo"
[3] "Regressing out Rn45s"
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|==== | 7%
|
|===== | 7%
|
|===== | 8%
|
|====== | 9%
|
|====== | 10%
|
|======= | 10%
|
|======= | 11%
|
|======== | 12%
|
|======== | 13%
|
|========= | 14%
|
|========== | 15%
|
|========== | 16%
|
|=========== | 16%
|
|=========== | 17%
|
|=========== | 18%
|
|============ | 18%
|
|============ | 19%
|
|============= | 20%
|
|============== | 21%
|
|============== | 22%
|
|=============== | 22%
|
|=============== | 23%
|
|=============== | 24%
|
|================ | 24%
|
|================ | 25%
|
|================= | 26%
|
|================= | 27%
|
|================== | 27%
|
|================== | 28%
|
|=================== | 29%
|
|=================== | 30%
|
|==================== | 31%
|
|===================== | 32%
|
|===================== | 33%
|
|====================== | 33%
|
|====================== | 34%
|
|======================= | 35%
|
|======================= | 36%
|
|======================== | 37%
|
|========================= | 38%
|
|========================= | 39%
|
|========================== | 39%
|
|========================== | 40%
|
|=========================== | 41%
|
|=========================== | 42%
|
|============================ | 43%
|
|============================ | 44%
|
|============================= | 44%
|
|============================= | 45%
|
|============================== | 46%
|
|=============================== | 47%
|
|=============================== | 48%
|
|================================ | 49%
|
|================================ | 50%
|
|================================= | 50%
|
|================================= | 51%
|
|================================== | 52%
|
|================================== | 53%
|
|=================================== | 54%
|
|==================================== | 55%
|
|==================================== | 56%
|
|===================================== | 56%
|
|===================================== | 57%
|
|====================================== | 58%
|
|====================================== | 59%
|
|======================================= | 60%
|
|======================================= | 61%
|
|======================================== | 61%
|
|======================================== | 62%
|
|========================================= | 63%
|
|========================================== | 64%
|
|========================================== | 65%
|
|=========================================== | 66%
|
|=========================================== | 67%
|
|============================================ | 67%
|
|============================================ | 68%
|
|============================================= | 69%
|
|============================================== | 70%
|
|============================================== | 71%
|
|=============================================== | 72%
|
|=============================================== | 73%
|
|================================================ | 73%
|
|================================================ | 74%
|
|================================================= | 75%
|
|================================================= | 76%
|
|================================================== | 76%
|
|================================================== | 77%
|
|================================================== | 78%
|
|=================================================== | 78%
|
|=================================================== | 79%
|
|==================================================== | 80%
|
|===================================================== | 81%
|
|===================================================== | 82%
|
|====================================================== | 82%
|
|====================================================== | 83%
|
|====================================================== | 84%
|
|======================================================= | 84%
|
|======================================================= | 85%
|
|======================================================== | 86%
|
|========================================================= | 87%
|
|========================================================= | 88%
|
|========================================================== | 89%
|
|========================================================== | 90%
|
|=========================================================== | 90%
|
|=========================================================== | 91%
|
|============================================================ | 92%
|
|============================================================ | 93%
|
|============================================================= | 93%
|
|============================================================= | 94%
|
|============================================================= | 95%
|
|============================================================== | 95%
|
|============================================================== | 96%
|
|=============================================================== | 97%
|
|================================================================ | 98%
|
|================================================================ | 99%
|
|=================================================================| 99%
|
|=================================================================| 100%
[1] "Scaling data matrix"
|
| | 0%
|
|=================================================================| 100%
tiss <- FindVariableGenes(object = tiss, do.plot = TRUE, x.high.cutoff = Inf, y.cutoff = 0.5)
Run Principal Component Analysis.
tiss <- RunPCA(object = tiss, do.print = FALSE)
tiss <- ProjectPCA(object = tiss, do.print = FALSE)
Later on (in FindClusters and TSNE) you will pick a number of principal components to use. This has the effect of keeping the major directions of variation in the data and, ideally, supressing noise. There is no correct answer to the number to use, but a decent rule of thumb is to go until the plot plateaus.
PCElbowPlot(object = tiss)
Choose the number of principal components to use.
# Set number of principal components.
n.pcs = 10
The clustering is performed based on a nearest neighbors graph. Cells that have similar expression will be joined together. The Louvain algorithm looks for groups of cells with high modularity–more connections within the group than between groups. The resolution parameter determines the scale…higher resolution will give more clusters, lower resolution will give fewer.
For the top-level clustering, aim to under-cluster instead of over-cluster. It will be easy to subset groups and further analyze them below.
# Set resolution
res.used <- 0.5
tiss <- FindClusters(object = tiss, reduction.type = "pca", dims.use = 1:n.pcs,
resolution = res.used, print.output = 0, save.SNN = TRUE)
To visualize
# If cells are too spread out, you can raise the perplexity. If you have few cells, try a lower perplexity (but never less than 10).
tiss <- RunTSNE(object = tiss, dims.use = 1:n.pcs, seed.use = 10, perplexity=30)
# note that you can set do.label=T to help label individual clusters
TSNEPlot(object = tiss, do.label = T)
Check expression of genes of interset.
Dotplots let you see the intensity of exppression and the fraction of cells expressing for each of your genes of interest.
How big are the clusters?
table(tiss@ident)
0 1 2 3 4 5
478 324 314 103 32 32
Which markers identify a specific cluster?
clust.markers <- FindMarkers(object = tiss, ident.1 = 0, only.pos = TRUE, min.pct = 0.25, thresh.use = 0.25)
print(x = head(x= clust.markers, n = 10))
p_val avg_diff pct.1 pct.2
Cd2 1.545932e-143 1.5170348 0.921 0.339
Tmsb10 9.639291e-130 0.8109003 0.985 0.740
Shisa5 1.166368e-125 1.2872957 0.950 0.442
Itm2a 2.530852e-101 2.4949185 0.680 0.173
Cd53 1.563433e-99 1.3559720 0.810 0.277
Ccr7 1.204113e-89 1.5242625 0.634 0.135
Pfn1 2.015557e-83 0.6569139 1.000 0.995
Cnn2 1.227621e-82 1.2236462 0.839 0.391
Actg1 1.278102e-75 0.8309641 0.985 0.871
Limd2 8.957058e-71 0.6781611 0.987 0.851
You can also compute all markers for all clusters at once. This may take some time.
#tiss.markers <- FindAllMarkers(object = tiss, only.pos = TRUE, min.pct = 0.25, thresh.use = 0.25)
Display the top markers you computed above.
#tiss.markers %>% group_by(cluster) %>% top_n(5, avg_diff)
At a coarse level, we can use canonical markers to match the unbiased clustering to known cell types:
0: thymocyte_1_mix of DN4_DP_SP 1: thymocyte_2_DP_favoring_CD8 differentiation 2: thymocyte_3_DP_favaroing_CD8 differentiation 3: thymoctye_4_DP_rapidly dividing_favoring CD8 differentation 4: stromal_mesenchymal cell 5: thymocyte_5_DN1
# stash current cluster IDs
tiss <- StashIdent(object = tiss, save.name = "cluster.ids")
# enumerate current cluster IDs and the labels for them
cluster.ids <- c(0, 1, 2, 3, 4, 5)
annotation <-
c(
"T cell",
"T cell",
"T cell",
"T cell",
"mesenchymal stem cell",
"T cell"
)
cell_ontology_id <-
c(
"CL:0000084",
"CL:0000084",
"CL:0000084",
"CL:0000084",
"CL:0000134",
"CL:0000084"
)
tiss@meta.data[,'annotation'] <- plyr::mapvalues(x = tiss@ident, from = cluster.ids, to = annotation)
tiss@meta.data[,'cell_ontology_id'] <- plyr::mapvalues(x = tiss@ident, from = cluster.ids, to = cell_ontology_id)
tiss@meta.data[tiss@cell.names,'annotation'] <- as.character(tiss@meta.data$annotation)
tiss@meta.data[tiss@cell.names,'cell_ontology_id'] <- as.character(tiss@meta.data$cell_ontology_id)
TSNEPlot(object = tiss, do.label = TRUE, pt.size = 0.5, group.by='annotation')
Color by metadata, like plate barcode, to check for batch effects.
TSNEPlot(object = tiss, do.return = TRUE, group.by = "plate.barcode")
Print a table showing the count of cells in each identity category from each plate.
table(as.character(tiss@ident), as.character(tiss@meta.data$plate.barcode))
B000827 B003284 B003285 B003291 MAA000607 MAA000609 MAA000880
0 244 0 2 36 106 35 55
1 6 0 3 45 166 57 47
2 16 1 4 195 10 22 66
3 3 1 0 8 39 30 22
4 22 0 0 3 3 1 3
5 1 5 11 4 7 1 3
We can repeat the above analysis on a subset of cells, defined using cluster IDs or some other metadata. This is a good way to drill down and find substructure.
# Subset data based on cluster id
subtiss <- SubsetData(object = tiss, ident.use = c(0), do.center = F, do.scale = F, cells.use = )
# To subset data based on annotation or other metadata, you can explicitly pass cell names
# anno = 'thymocyte'
# cells.to.use = tiss@cell.names[which(tiss@meta.data$annotation == anno)]
# subtiss <- SubsetData(object = tiss, cells.use = cells.to.use, do.center = F, do.scale = F)
subtiss <- NormalizeData(object = subtiss)
subtiss <- ScaleData(object = subtiss, vars.to.regress = c("nReads", "percent.ribo","Rn45s"))
[1] "Regressing out nReads" "Regressing out percent.ribo"
[3] "Regressing out Rn45s"
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|==== | 7%
|
|===== | 7%
|
|===== | 8%
|
|====== | 9%
|
|====== | 10%
|
|======= | 10%
|
|======= | 11%
|
|======== | 12%
|
|======== | 13%
|
|========= | 14%
|
|========== | 15%
|
|========== | 16%
|
|=========== | 16%
|
|=========== | 17%
|
|=========== | 18%
|
|============ | 18%
|
|============ | 19%
|
|============= | 20%
|
|============== | 21%
|
|============== | 22%
|
|=============== | 22%
|
|=============== | 23%
|
|=============== | 24%
|
|================ | 24%
|
|================ | 25%
|
|================= | 26%
|
|================= | 27%
|
|================== | 27%
|
|================== | 28%
|
|=================== | 29%
|
|=================== | 30%
|
|==================== | 31%
|
|===================== | 32%
|
|===================== | 33%
|
|====================== | 33%
|
|====================== | 34%
|
|======================= | 35%
|
|======================= | 36%
|
|======================== | 37%
|
|========================= | 38%
|
|========================= | 39%
|
|========================== | 39%
|
|========================== | 40%
|
|=========================== | 41%
|
|=========================== | 42%
|
|============================ | 43%
|
|============================ | 44%
|
|============================= | 44%
|
|============================= | 45%
|
|============================== | 46%
|
|=============================== | 47%
|
|=============================== | 48%
|
|================================ | 49%
|
|================================ | 50%
|
|================================= | 50%
|
|================================= | 51%
|
|================================== | 52%
|
|================================== | 53%
|
|=================================== | 54%
|
|==================================== | 55%
|
|==================================== | 56%
|
|===================================== | 56%
|
|===================================== | 57%
|
|====================================== | 58%
|
|====================================== | 59%
|
|======================================= | 60%
|
|======================================= | 61%
|
|======================================== | 61%
|
|======================================== | 62%
|
|========================================= | 63%
|
|========================================== | 64%
|
|========================================== | 65%
|
|=========================================== | 66%
|
|=========================================== | 67%
|
|============================================ | 67%
|
|============================================ | 68%
|
|============================================= | 69%
|
|============================================== | 70%
|
|============================================== | 71%
|
|=============================================== | 72%
|
|=============================================== | 73%
|
|================================================ | 73%
|
|================================================ | 74%
|
|================================================= | 75%
|
|================================================= | 76%
|
|================================================== | 76%
|
|================================================== | 77%
|
|================================================== | 78%
|
|=================================================== | 78%
|
|=================================================== | 79%
|
|==================================================== | 80%
|
|===================================================== | 81%
|
|===================================================== | 82%
|
|====================================================== | 82%
|
|====================================================== | 83%
|
|====================================================== | 84%
|
|======================================================= | 84%
|
|======================================================= | 85%
|
|======================================================== | 86%
|
|========================================================= | 87%
|
|========================================================= | 88%
|
|========================================================== | 89%
|
|========================================================== | 90%
|
|=========================================================== | 90%
|
|=========================================================== | 91%
|
|============================================================ | 92%
|
|============================================================ | 93%
|
|============================================================= | 93%
|
|============================================================= | 94%
|
|============================================================= | 95%
|
|============================================================== | 95%
|
|============================================================== | 96%
|
|=============================================================== | 97%
|
|================================================================ | 98%
|
|================================================================ | 99%
|
|=================================================================| 99%
|
|=================================================================| 100%
[1] "Scaling data matrix"
|
| | 0%
|
|=================================================================| 100%
subtiss <- FindVariableGenes(object = subtiss, do.plot = TRUE, x.high.cutoff = Inf, y.cutoff = 0.8)
subtiss <- RunPCA(object = subtiss, pcs.compute = 20)
[1] "PC1"
[1] "Satb1" "Myb" "Itm2a" "Sox4" "Tcf7" "Tox"
[7] "Cd24a" "Ccr9" "Sept11" "Hivep3" "Cd27" "Slc29a1"
[13] "Vamp1" "Ldhb" "Lars2" "Jarid2" "Hspa4l" "Colq"
[19] "BC024659" "Sf3b3" "Pfkp" "Cnn3" "Arpp21" "Nedd4"
[25] "Tacc2" "Ikzf2" "Mtf2" "Camk4" "Ppan" "Kcnq1ot1"
[1] ""
[1] "H2-K1" "B2m" "Ms4a4b" "Shisa5" "Gimap3" "AW112010"
[7] "Stat1" "Ms4a6b" "Ltb" "Tgtp2" "Itgb7" "Ifit1"
[13] "Zbp1" "Ly6a" "Ccr6" "H2-Q6" "Tgtp1" "Irgm1"
[19] "Rln3" "Foxp3" "Capg" "Pydc3" "Dhx58" "S1pr1"
[25] "Ctla4" "Ccr7" "Tnfrsf4" "Ifi47" "Ccnd2" "Tap1"
[1] ""
[1] ""
[1] "PC2"
[1] "Cd8b1" "Cd8a" "Ccr9" "Dntt"
[5] "Bcl11b" "Slc16a5" "Satb1" "Tcf7"
[9] "1190002H23Rik" "Arpp21" "Rmnd5a" "Rag1"
[13] "2610019F03Rik" "Lpar6" "Fbxl12" "Aqp11"
[17] "Rcbtb2" "Tgtp1" "Ssbp2" "Pdk1"
[21] "Tube1" "Gbp8" "Trib2" "Zcchc11"
[25] "Rag2" "Kcnh3" "Prkcb" "Nkg7"
[29] "Rhoh" "Mns1"
[1] ""
[1] "Tnfrsf9" "Srgn" "Cd5" "Pdcd1"
[5] "Nr4a1" "Tnfrsf4" "Stx11" "Nfkbid"
[9] "Ikzf2" "Tagap" "Tigit" "Rln3"
[13] "Tnfrsf18" "Nr4a3" "Cd6" "Itm2a"
[17] "Hivep3" "Gadd45b" "Ikzf3" "Ccr8"
[21] "Acot7" "Ccr6" "Cytip" "Gata3"
[25] "A430093F15Rik" "Chst2" "Ctla4" "Sdcbp2"
[29] "Ephx1" "Icos"
[1] ""
[1] ""
[1] "PC3"
[1] "Ldhb" "Nfkbia" "Gata3" "Lpar6"
[5] "Cnn3" "Btg2" "Dusp1" "Copz1"
[9] "Ltb" "Sdc4" "Itm2a" "Aqp11"
[13] "Bcl2l11" "Ttc19" "Lyrm5" "Dcn"
[17] "Tspan32" "Zfp689" "Fam118b" "Commd10"
[21] "Nmnat3" "8430410A17Rik" "Cd27" "Fbxl12"
[25] "Asb6" "Acp5" "H2-Q2" "Bambi-ps1"
[29] "Tab1" "Rad9"
[1] ""
[1] "Lars2" "Nkg7" "Esyt1" "Atp1a1" "Runx3" "Itgae"
[7] "Pydc3" "Ash1l" "Gpr114" "Eif2s3y" "Tgtp1" "Heatr1"
[13] "Mthfd1" "Lcp1" "Stat1" "Gm4759" "Tgtp2" "Nampt"
[19] "Gemin4" "Akna" "Heatr3" "Kcnq1ot1" "Ctsw" "Epm2aip1"
[25] "Irgm1" "Top2b" "Eef2" "Cmtm6" "Ptk2b" "Nol9"
[1] ""
[1] ""
[1] "PC4"
[1] "Itm2a" "Id2" "Tuba4a" "Ccr7" "Satb1" "Il7r" "Cytip"
[8] "Acvrl1" "Gpatch1" "Sgms1" "Bcl2" "Bzw2" "Samhd1" "Tbl1x"
[15] "Als2cl" "Gsn" "Lef1" "Tnfsf11" "Crtc1" "Bcl11b" "Folr4"
[22] "Arhgef6" "Telo2" "Stradb" "Gata3" "Tecpr1" "Arhgef4" "Shc1"
[29] "Xylt2" "Mn1"
[1] ""
[1] "Ikzf2" "Cd8a" "Cd8b1" "Myb" "Colq"
[6] "Arpp21" "AW112010" "Nkg7" "Srgn" "Dntt"
[11] "Rmnd5a" "Itgae" "Aqp11" "Ifngr1" "Cd24a"
[16] "Tigit" "Ly6d" "Tnfrsf9" "Sdc4" "Tagap"
[21] "Slc16a5" "Cst7" "Rag1" "H2-Q6" "Nfkbia"
[26] "Ly6e" "Tagln2" "Lig4" "Trp53inp1" "Stx11"
[1] ""
[1] ""
[1] "PC5"
[1] "Pld3" "Nkg7" "Cct3" "1190002H23Rik"
[5] "Ptpn6" "Tagln2" "Bcl2" "Prep"
[9] "Mcm5" "Gpr114" "Cirh1a" "Hivep3"
[13] "Smad2" "Id3" "Cd8a" "Lcp1"
[17] "Ddx3x" "Runx3" "Rpn2" "Tuba4a"
[21] "Nampt" "Ppat" "Filip1l" "Cd8b1"
[25] "Anapc5" "Pold1" "Sla" "Rrp1b"
[29] "Agpat2" "Tubb3"
[1] ""
[1] "E430025E21Rik" "Csnk2a2" "Ap2a1" "Zfp592"
[5] "Glt25d1" "Aff1" "Terf2" "Arhgap17"
[9] "Cpsf4" "Fam132a" "Spred1" "Zfp192"
[13] "Epm2aip1" "Gfm2" "Cog6" "Rrbp1"
[17] "Zfp85-rs1" "Zswim4" "Adcy6" "BC024659"
[21] "Zfp628" "Scai" "Ash1l" "Phf8"
[25] "Mtap4" "Edc4" "Synj1" "Sall2"
[29] "Terf1" "Rtf1"
[1] ""
[1] ""
subtiss <- ProjectPCA(object = subtiss, do.print = FALSE)
Run Principal Component Analysis.
subtiss <- RunPCA(object = subtiss, do.print = FALSE)
subtiss <- ProjectPCA(object = subtiss, do.print = FALSE)
# If this fails for your subset, it may be that cells.use is more cells than you have left! Try reducing it.
PCHeatmap(object = subtiss, pc.use = 1:3, cells.use = 200, do.balanced = TRUE, label.columns = FALSE, num.genes = 12)
Later on (in FindClusters and TSNE) you will pick a number of principal components to use. This has the effect of keeping the major directions of variation in the data and, ideally, supressing noise. There is no correct answer to the number to use, but a decent rule of thumb is to go until the plot plateaus.
PCElbowPlot(object = subtiss)
Choose the number of principal components to use.
# Set number of principal components.
sub.n.pcs = 6
The clustering is performed based on a nearest neighbors graph. Cells that have similar expression will be joined together. The Louvain algorithm looks for groups of cells with high modularity–more connections within the group than between groups. The resolution parameter determines the scale…higher resolution will give more clusters, lower resolution will give fewer.
# Set resolution
sub.res.used <- 1
subtiss <- FindClusters(object = subtiss, reduction.type = "pca", dims.use = 1:sub.n.pcs,
resolution = sub.res.used, print.output = 0, save.SNN = TRUE)
To visualize
# If cells are too spread out, you can raise the perplexity. If you have few cells, try a lower perplexity (but never less than 10).
subtiss <- RunTSNE(object = subtiss, dims.use = 1:sub.n.pcs, seed.use = 10, perplexity=20)
# note that you can set do.label=T to help label individual clusters
TSNEPlot(object = subtiss, do.label = T)
subtiss.markers <- FindAllMarkers(object = subtiss, only.pos = TRUE, min.pct = 0.25, thresh.use = 0.25)
subtiss.markers %>% group_by(cluster) %>% top_n(6, avg_diff)
Check expression of genes of interset.
genes_to_check = c('Cd4','Cd8a','Cd53','Cd44','Cd7','Cd2','Il2ra','Rag1','Zap70','Dntt','Il7r','Cd69','Pdcd1','Shisa5','Cd5','Lef1','Ccr7','Ptcra','Itm2a','Cd28','Cd3e')
FeaturePlot(subtiss, genes_to_check, pt.size = 1)
Dotplots let you see the intensity of exppression and the fraction of cells expressing for each of your genes of interest.
How big are the clusters?
table(subtiss@ident)
0 1 2 3 4 5
104 97 92 79 62 44
Color by metadata, like plate barcode, to check for batch effects.
TSNEPlot(object = subtiss, do.return = TRUE, group.by = "plate.barcode")
Print a table showing the count of cells in each identity category from each plate.
table(as.character(subtiss@ident), as.character(subtiss@meta.data$plate.barcode))
B000827 B003285 B003291 MAA000607 MAA000609 MAA000880
0 58 0 1 25 6 14
1 78 0 5 4 5 5
2 50 2 8 10 7 15
3 24 0 5 29 8 13
4 32 0 1 18 5 6
5 2 0 16 20 4 2
For the subsets, we produce subannotations. These will be written back as metadata in the original object, so we can see all subannotations together.
If some of the clusters you find in the subset deserve additional annotation, you can add that right here. Use NA for clusters for which no subannotation is needed.
subcluster.ids <- c(0, 1, 2,3,4,5)
subannotation <- c("immature_SP_CD4","immature_SP_CD4","DN4","immature_SP_CD4","Immature_SP_CD8","DP")
subtiss@meta.data[,'subannotation'] <- plyr::mapvalues(x = subtiss@ident, from = subcluster.ids, to = subannotation)
tiss@meta.data[subtiss@cell.names,'subannotation'] <- as.character(subtiss@meta.data$subannotation)
TSNEPlot(object = subtiss, do.label = TRUE, pt.size = 0.5, group.by='subannotation')
When you save the annotated tissue, please give it a name.
filename = here('00_data_ingest', 'tissue_seurat_robj',
paste0(tissue_of_interest, "_seurat_tiss.Robj"))
print(filename)
[1] "/Users/olgabot/code/tabula-muris/00_data_ingest/tissue_seurat_robj/Thymus_seurat_tiss.Robj"
save(tiss, file=filename)
# To reload a saved object
# filename = here('00_data_ingest', 'tissue_seurat_robj',
# paste0(tissue_of_interest, "_seurat_tiss.Robj"))
# load(file=filename)
So that Biohub can easily combine all your annotations, please export them as a simple csv.
head(tiss@meta.data)
filename = here('00_data_ingest', 'tissue_annotation_csv',
paste0(tissue_of_interest, "_annotation.csv"))
write.csv(tiss@meta.data[,c('plate.barcode','annotation','cell_ontology_id')], file=filename)